package com.canbot.u05.activity.vipguide;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.ToastTools;


/**
 * Created by cwf on 2017/5/26.
 * 设置贵宾尊称
 */

public class SetVipNameActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private EditText vipName;

        private String newName;

        private String oldVipName;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_vip_name_setting);

                oldVipName = getIntent().getStringExtra("vipname");
                if(TextUtils.isEmpty(getIntent().getStringExtra("isSetting"))){
                        MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.VIP_GUIDE_DIR + "u05_scgb_001.wav");
                }else{
                        playSound();
                }
                setViews();
        }

        private void setViews() {
                findViewById(R.id.imageView_back).setOnClickListener(this);
                findViewById(R.id.ok_btn).setOnClickListener(this);
                vipName = (EditText) findViewById(R.id.et_vip_name);

                if(TextUtils.isEmpty(getIntent().getStringExtra("isSetting"))){
                        findViewById(R.id.text_vip_name).setVisibility(View.GONE);
                        vipName.setText(getIntent().getStringExtra("name"));
                }else{
                        vipName.setText(oldVipName);
                }
        }

        private void playSound() {
                String[] voices = {"u05_xmqr_001.wav", "u05_xmqr_002.wav", "u05_xmqr_003.wav"};
                int index = (int) (Math.random() * voices.length);

                MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.VIP_GUIDE_DIR + voices[index]);
                MsgSendUtils.sendStringMsg(MsgType.EYE_MOTION, "34");
                if (index == 0) {
                        MsgSendUtils.sendStringMsg(MsgType.ACTION, "14009");
                }
                else if (index == 1) {
                        MsgSendUtils.sendStringMsg(MsgType.ACTION, "14007");
                }
                else {
                        MsgSendUtils.sendStringMsg(MsgType.ACTION, "14006");
                }
        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.ok_btn:
                                newName = vipName.getText().toString().trim();
                                if (newName.isEmpty()) {
                                        ToastTools.newInstance(SetVipNameActivity.this).showText(getString(R.string.please_set_your_name));
                                }
                                else {
                                        if(TextUtils.isEmpty(getIntent().getStringExtra("isSetting"))){

                                                startActivity(new Intent(SetVipNameActivity.this, VipGuideConfirmActivity.class)
                                                        .putExtra("newvipname", newName)
                                                        .putExtra("vipfacepath", getIntent().getStringExtra("path"))
                                                        .putExtra("identification",getIntent().getStringExtra("identification")));

                                        }else{
                                                startActivity(new Intent(SetVipNameActivity.this, VipGuideConfirmActivity.class)
                                                        .putExtra("newvipname", newName)
                                                        .putExtra("vipfacepath", getIntent().getStringExtra("facepath"))
                                                        .putExtra("identification",getIntent().getStringExtra("identification"))
                                                        .putExtra("isSetting",getIntent().getStringExtra("isSetting")));
                                        }
                                }

                                break;
                        case R.id.imageView_back:
                                finish();
                        default:
                                break;
                }
        }

        @Override
        protected void onPause() {
                super.onPause();
                MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stop_sound");
        }
}
